/**
 * 文件名：LogisticsVehicleFacadeImpl.java
 *
 * 创建人：Fyh - 1009173473@qq.com
 *
 * 创建时间：2019年9月25日 下午5:18:40
 *
 * 版权所有：南京植百汇智能科技服务有限公司
 */
package com.z100h.supplychain.evidence.facade.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.z100h.supplychain.core.VehicleState;
import com.z100h.supplychain.evidence.entity.LogisticsVehicle;
import com.z100h.supplychain.evidence.entity.LogisticsVehicleListVo;
import com.z100h.supplychain.evidence.entity.Worker;
import com.z100h.supplychain.evidence.facade.LogisticsVehicleFacade;
import com.z100h.supplychain.evidence.service.LogisticsVehicleDetailService;
import com.z100h.supplychain.evidence.service.LogisticsVehicleService;
import com.z100h.supplychain.evidence.service.StockoutService;

/**
 * [描述信息：说明类的基本功能]
 *
 * @author Fyh - 1009173473@qq.com
 * @version 1.0 Created on 2019年9月25日 下午5:18:40
 */
@Service
public class LogisticsVehicleFacadeImpl implements LogisticsVehicleFacade {

	@Autowired
	private LogisticsVehicleService logisticsVehicleService;

	@Autowired
	private LogisticsVehicleDetailService logisticsVehicleDetailService;

	@Autowired
	private StockoutService stockoutService;

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.z100h.supplychain.evidence.facade.LogisticsVehicleFacade#findByPage(com.jeeplus.core.persistence.Page, com.z100h.supplychain.evidence.entity.LogisticsVehicle)
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月25日 下午5:33:04
	 */
	@Override
	public Page<LogisticsVehicle> findByPage(Page<LogisticsVehicle> page, LogisticsVehicle logisticsVehicle) {
		return logisticsVehicleService.findByPage(page, logisticsVehicle);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.z100h.supplychain.evidence.facade.LogisticsVehicleFacade#findByLogisticsVehicleList()
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月26日 上午10:19:56
	 */
	@Override
	public List<LogisticsVehicleListVo> findByLogisticsVehicleList() {
		return logisticsVehicleService.findByLogisticsVehicleList();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.z100h.supplychain.evidence.facade.LogisticsVehicleFacade#save(com.z100h.supplychain.evidence.entity.LogisticsVehicle, java.util.List)
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月26日 下午5:34:52
	 */
	@Override
	@Transactional(readOnly = false)
	public void save(LogisticsVehicle logisticsVehicle, List<LogisticsVehicleListVo> logisticsVehicleList,
			String workerId) {
		// 插入主表信息
		logisticsVehicle.setAssignor(workerId);
		logisticsVehicleService.insert(logisticsVehicle);

		// 插入子表信息
		if (CollectionUtils.isNotEmpty(logisticsVehicleList)) {
			for (LogisticsVehicleListVo l : logisticsVehicleList) {
				logisticsVehicleDetailService.insertLogisticsVehicleDetail(l.getCustomerId(), l.getServiceType(),
						logisticsVehicle.getId());
			}

			// 复壮指派司机
			List<String> eids = logisticsVehicleDetailService.findRepairEidsByVehicleId(logisticsVehicle.getId());
			if (CollectionUtils.isNotEmpty(eids)) {
				List<String> repairIds = stockoutService.findRepairIdsByEids(eids);
				if (CollectionUtils.isNotEmpty(repairIds)) {
					logisticsVehicleService.updateRepairAssign(workerId, logisticsVehicle.getDeliveryDate(), repairIds);
				}
			}
		}

	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.z100h.supplychain.evidence.facade.LogisticsVehicleFacade#findNotLoadByPage(com.jeeplus.core.persistence.Page, com.z100h.supplychain.evidence.entity.LogisticsVehicle)
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年10月21日 下午3:12:23
	 */
	@Override
	public Page<LogisticsVehicle> findLoadByPage(Page<LogisticsVehicle> page, LogisticsVehicle logisticsVehicle) {
		return logisticsVehicleService.findLoadByPage(page, logisticsVehicle);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.z100h.supplychain.evidence.facade.LogisticsVehicleFacade#finish(java.util.List)
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年10月21日 下午3:48:05
	 */
	@Override
	@Transactional(readOnly = false)
	public void finish(List<String> ids) {
		logisticsVehicleService.finish(ids, VehicleState.FINISH.getType(), UserUtils.getUser().getId());
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.z100h.supplychain.evidence.facade.LogisticsVehicleFacade#findSjType()
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年10月22日 上午11:21:58
	 */
	@Override
	public List<Worker> findSjType() {
		return logisticsVehicleService.findWorkerListByPostType("siji");
	}

}
